package com.summer.LanQiao;

import java.util.Arrays;

public class Test {
    static void SelectSort(int[] arr){
        for (int i = 0; i < arr.length-1 ; i++) {
            int minIndex = i;
            int min = arr[minIndex];
            for (int j = i+1; j <arr.length ; j++) {
                if (min>arr[j]){
                    min = arr[j];
                    minIndex = j;
                }
            }
            arr[minIndex] = arr[i];
            arr[i] = min;
        }
    }
    public static int findUnsortedSubarray(int[] nums) {
        int [] num1 = new int[nums.length];
        for (int i = 0; i < nums.length ; i++) {
            num1[i] = nums[i];
        }
        System.out.println(Arrays.toString(num1));
        Arrays.sort(nums);
        //SelectSort(nums);
        System.out.println(Arrays.toString(nums));
        int count = 0;
        boolean flag = false;
        int indexH = 0;
        int indexE = nums.length-1;
        boolean flagH = false;
        boolean flagE = false;
        while(true){
            if(indexH>=indexE||flagE&&flagH) break;
            if(num1[indexH]!=nums[indexH]) flagH = true;
            else indexH++;
            if(num1[indexE]!=nums[indexE]) flagE = true;
            else indexE--;
        }
        System.out.println(indexE+"---"+indexH);
        count = indexE-indexH+1;
        return count;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{2,6,4,8,10,9,15};
        System.out.println(findUnsortedSubarray(arr));
    }
}
